/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.project.d.network.msg.serializer;


import org.project.d.network.msg.enums.SerializationType;

public class SerializationManager {

    private static Serialization[] serializers = new Serialization[5];

    static {
        addSerializer(SerializationType.UNKNOWN.getType(), SerializationType.UNKNOWN.getSerialization());
        addSerializer(SerializationType.JSON.getType(), SerializationType.JSON.getSerialization());
        addSerializer(SerializationType.PROTOBUF.getType(), SerializationType.PROTOBUF.getSerialization());
    }

    public static Serialization getSerializer(byte idx) {
        if (idx<serializers.length&&idx>=0&&serializers[idx]!=null){
            return serializers[idx];
        }
        throw new IllegalStateException("Unknown SerializationType: " + idx);
    }

    public static void addSerializer(byte idx, Serialization serializer) {
        if (serializers.length>Byte.MAX_VALUE){
            throw new RuntimeException("serializers idx or count must <= "+Byte.MAX_VALUE);
        }
        if (serializers.length <= idx) {
            Serialization[] newSerializers = new Serialization[idx + 5];
            System.arraycopy(serializers, 0, newSerializers, 0, serializers.length);
            serializers = newSerializers;
        }
        serializers[idx] = serializer;
    }
}
